import { defineStore } from 'pinia'
import { ref } from 'vue'
import axios from '../plugins/axios'
import { Storage } from '../utils/localStorage'

const storage = new Storage()

export const useUserStore = defineStore('userStore', () => {
   const userInfo = ref<UserModel>()

   const stora = storage.getStorage('userInfo') as UserModel
   userInfo.value = stora ? stora : ({} as UserModel)

   const getUserInfo = async () => {
      const res = await axios.request<UserModel>({ url: 'user' })

      userInfo.value = res.data
      storage.setStorage('userInfo', userInfo.value)

      return res
   }

   return { userInfo, getUserInfo }
})
